<?php

/**
 * lay toan thuong hieu theo id danh muc
 * @param int $cate_id
 * @return array $header, $rs, $pager
 * @author anhmantk
 * @date Dec 29, 2011
 */
function getAllBrandByCateId($cate_id) {
    
    $header = array(
        array('data' => 'STT', 'width' => '30'),
        array('data' => 'Thương hiệu', 'field' => 'brand_name'),
        array('data' => 'Logo', 'width' => '200'),        
        array('data' => 'Tên viết tắt', 'field' => 'brand_index', 'width' => '100'),
        array('data' => 'Độ ưu tiên', 'field' => 'brand_position', 'sort' => 'asc', 'width' => '85'),
        array('data' => 'Trạng thái', 'width' => '100'),
        array('data' => 'Sửa', 'width' => '30'),
        array('data' => 'Khóa', 'width' => '45')
    );
    
    $where = " cate_id = " . $cate_id;
    $query = "SELECT * FROM {category_brand} WHERE " . $where . " " . tablesort_sql($header);
    $count_query = "SELECT COUNT(brand_id) FROM {category_brand} WHERE " . $where;
    
    $result = pager_query($query, LIMIT_PAGER, 0, $count_query);    
    while(($obj = db_fetch_object($result)) != null) {
        $rs[] = $obj;
    }
    
    $pager = theme('pager');
    return array($header, $rs, $pager);
}

/**
 * lay lai thong tin thuong hieu theo id
 * @param int $brand_id
 * @return stdClass thong tin thuong hieu
 * @author anhmantk
 * @date Dec 29, 2011
 */
function getBrandById($brand_id) {
    $query = "SELECT * FROM {category_brand} WHERE brand_id = %d";
    return db_fetch_object(db_query($query, $brand_id));
}

/**
 * them moi thuong hieu
 * @param array $params
 * array(`cate_id`, `file_path`, `brand_name`, `brand_index`)
 * @return boolean
 * @author anhmantk
 * @date Dec 29, 2011
 */
function insertBrand($params = array()) {
    $query = "INSERT INTO {category_brand} (`cate_id`, `file_path`, `brand_name`, `brand_index`, `brand_status`, `brand_position`) VALUES (%d, '%s', '%s', '%s', %d, %d)";
    return db_query($query, $params);
}

/**
 * sua thuong hieu
 * @param array $params 
 * array(`file_path`, `brand_name`, `brand_index`, `brand_id`)
 * @return boolean
 * @author anhmantk
 * @date Dec 29, 2011
 */
function editBrand($params = array()) {
    $query = "UPDATE {category_brand} SET `file_path` = '%s', `brand_name` = '%s', `brand_index` = '%s', `brand_status` = %d, `brand_position` = %d WHERE brand_id = %d";
    return db_query($query, $params);
}

/**
 * xoa thuong hieu
 * @param int $brand_id
 * @return boolean
 * @author anhmantk
 * @date Dec 29, 2011
 */
function removeBrand($brand_id) {
    $query = "DELETE FROM {category_brand} WHERE brand_id = %d";
    return db_query($query, $brand_id);
}

/**
 * khoa hoac bo khoa thuong hieu
 * @param int $status
 * @param int $brand_id
 * @return boolean
 * @author anhmantk
 * @date Dec 30, 2011
 */
function lockUnlockBrand($status  = 0,$brand_id) {
    $query = "UPDATE {category_brand} SET brand_status = %d WHERE brand_id = %d";
    return db_query($query, array($status, $brand_id));
}